﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccessLayer;
using BussinessLayer;

namespace QLBaoHanh.DoiBHSuaChua
{
    public partial class frmGuiNhaCungCao : Form
    {
        private List<View_ThietBiBHDetail> _phien_ThietBiBHList;
        private frmDsThietBiDoiBHSC _callerForm;
        public frmGuiNhaCungCao(List<View_ThietBiBHDetail> inPhien_ThietBiBHList, frmDsThietBiDoiBHSC inCallerForm)
        {
            InitializeComponent();
            _phien_ThietBiBHList = inPhien_ThietBiBHList;
            _callerForm = inCallerForm;
        }
        private frmGuiNhaCungCao()
        {
            InitializeComponent();
        }
        private void LoadForm()
        {
            // Fill combobox nhan vien thuc hien
            cboNhaCungCap.DataSource = NhaCungCapBO.getInstance.GetAllNhaCungCap();
            cboNhaCungCap.DisplayMember = "MaTenNhaCungCap";
            cboNhaCungCap.ValueMember = "Id";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            foreach (View_ThietBiBHDetail item in _phien_ThietBiBHList)
            {
                //// Them vao bang ThietBiBaoHanh_NhanVienThucHien
                //ThietBiBH_NhanVienThucHien obj = new ThietBiBH_NhanVienThucHien();
                //obj.Phien_ThietBiBHId = item.Id;
                //obj.NhanVienThucHienId = (int)cboNhaCungCap.SelectedValue;
                //obj.ThoiDiemGiao = dtpNgayGiao.Value;
                //obj.NoiDung = txtGhiChu.Text;
                //ThietBiBH_NhanVienThucHienBO.getInstance.InsertThietBiBH_NhanVienThucHien(obj);

                // Chuyen trang thai Phien_ThietBiBH sang dang nha cung cap
                Phien_ThietBiBH aPhien_TBBH = Phien_ThietBiBHBO.getInstance.GetPhien_ThietBiBHById((int)item.Id);
                aPhien_TBBH.TrangThaiThietBiBHID = (int)MyCommon.TrangThaiThietBiBH.DangONCC;
                Phien_ThietBiBHBO.getInstance.UpdatePhien_ThietBiBH(aPhien_TBBH);

                //Ghi nhan lich su bao hanh
                LichSuBH objLichSu = new LichSuBH();
                objLichSu.LoaiLichSu = (int)MyCommon.LoaiLichSu.LichSuHeThong;
                objLichSu.Phien_ThietBiBHId = Phien_ThietBiBHBO.getInstance.GetLastRecord().Id;
                objLichSu.NVThucHienId = frmMain.CurrentNhanVien.Id;
                objLichSu.ThoiGian = DateTime.Now;
                objLichSu.NoiDung = "Gửi tới nhà cung cấp " + cboNhaCungCap.Text;
                LichSuBHBO.getInstance.InsertLichSuBH(objLichSu);

            }
            
            _callerForm.RefreshForm();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmGuiNhaCungCao_Load(object sender, EventArgs e)
        {
            LoadForm();
        }
    }
}
